package com.giscloud.amp.service.sys.impl;

import com.alibaba.fastjson.JSON;
import com.giscloud.amp.dao.AmpChangeLogMapper;
import com.giscloud.amp.dao.AmpStaffMapper;
import com.giscloud.amp.entity.AmpChangeLogExample;
import com.giscloud.amp.entity.AmpStaffExample;
import com.giscloud.amp.entity.dto.SysLogDTO;
import com.giscloud.amp.entity.vo.AmpChangeLogVO;
import com.giscloud.amp.entity.vo.AmpStaffVO;
import com.giscloud.amp.service.sys.SysOperateLogService;
import com.giscloud.datasource.page.PageInfo;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:gxd
 * @Description:
 * @Date: 9:43 2018/3/9
 * @Modified By:
 */
@Service("sysOperateLogService")
public class SysOperateLogServiceImpl implements SysOperateLogService {
    private final static Logger log = LoggerFactory.getLogger(SysOperateLogServiceImpl.class);
    @Autowired
    private AmpStaffMapper ampStaffMapper;
    @Autowired
    private AmpChangeLogMapper ampChangeLogMapper;
    @Override
    public PageInfo getList(SysLogDTO sysLogDTO) {
        log.debug("#getSysOperateLogList parameter ={}", JSON.toJSONString(sysLogDTO));
        PageHelper.startPage(sysLogDTO.getPage(), sysLogDTO.getRows());
        AmpChangeLogExample ampChangeLogExample = new AmpChangeLogExample();
        AmpChangeLogExample.Criteria criteria = ampChangeLogExample.createCriteria();
        if(StringUtils.isNotBlank(sysLogDTO.getObjectType())){
            criteria.andObjectTypeEqualTo(sysLogDTO.getObjectType());
        }
        if(StringUtils.isNotBlank(sysLogDTO.getOperateType())){
            criteria.andOperateTypeEqualTo(sysLogDTO.getOperateType());
        }
        ampChangeLogExample.setOrderByClause("OPERATE_DATE DESC");
        criteria.andOperateDateBetween(sysLogDTO.getStartDate(),sysLogDTO.getEndDate());
        List<AmpChangeLogVO> ampChangeLogVOs = ampChangeLogMapper.selectByExample(ampChangeLogExample);
        for(int i=0;i<ampChangeLogVOs.size();i++){
            log.error("====="+ampChangeLogVOs.get(i).getLogId());
        }
        AmpStaffExample ampStaffExample = new AmpStaffExample();
        List<AmpStaffVO> ampStaffVOS = ampStaffMapper.selectByExampleWhere(ampStaffExample);
        Map staffNameMap = new HashMap();
        for (AmpStaffVO ampStaffVO : ampStaffVOS) {
            staffNameMap.put(ampStaffVO.getStaffId(),ampStaffVO.getStaffName());
        }
        for (AmpChangeLogVO ampChangeLogVO : ampChangeLogVOs) {
            if(staffNameMap.get(ampChangeLogVO.getOperateStaffId()) != null) {
                ampChangeLogVO.setStaffName(staffNameMap.get(ampChangeLogVO.getOperateStaffId()).toString());
            }
        }
        return PageInfo.of(ampChangeLogVOs);
    }

    @Override
    public int delete(Long logId) {
        log.debug("#delete parameter ={}", logId);
        AmpChangeLogExample ampChangeLogExample = new AmpChangeLogExample();
        AmpChangeLogExample.Criteria criteria = ampChangeLogExample.createCriteria();
        if(logId != null){
            criteria.andLogIdEqualTo(logId);
        }
        return ampChangeLogMapper.deleteByExample(ampChangeLogExample);
    }
}
